package com.ruoyi.reportTask.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.reportTask.domain.IndustrialReportTaskCenter;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.reportTask.domain.bo.IndustrialReportTaskBo;
import com.ruoyi.reportTask.domain.vo.IndustrialReportTaskVo;
import com.ruoyi.reportTask.domain.IndustrialReportTask;
import com.ruoyi.reportTask.mapper.IndustrialReportTaskMapper;
import com.ruoyi.reportTask.service.IIndustrialReportTaskService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 填报任务Service业务层处理
 *
 * @author yangzhen
 * @date 2025-11-13
 */
@RequiredArgsConstructor
@Service
public class IndustrialReportTaskServiceImpl implements IIndustrialReportTaskService {

    private final IndustrialReportTaskMapper baseMapper;

    /**
     * 查询填报任务
     */
    @Override
    public IndustrialReportTaskVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询填报任务列表
     */
    @Override
    public TableDataInfo<IndustrialReportTaskVo> queryPageList(IndustrialReportTaskBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<IndustrialReportTask> lqw = buildQueryWrapper(bo);
        Page<IndustrialReportTaskVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询填报任务列表
     */
    @Override
    public List<IndustrialReportTaskVo> queryList(IndustrialReportTaskBo bo) {
        LambdaQueryWrapper<IndustrialReportTask> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<IndustrialReportTask> buildQueryWrapper(IndustrialReportTaskBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<IndustrialReportTask> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getTaskName()), IndustrialReportTask::getTaskName, bo.getTaskName());
        lqw.eq(StringUtils.isNotBlank(bo.getReportObj()), IndustrialReportTask::getReportObj, bo.getReportObj());
        lqw.eq(StringUtils.isNotBlank(bo.getReportCycle()), IndustrialReportTask::getReportCycle, bo.getReportCycle());
        lqw.eq(bo.getSendDate() != null, IndustrialReportTask::getSendDate, bo.getSendDate());
        lqw.eq(StringUtils.isNotBlank(bo.getSendDateStr()), IndustrialReportTask::getSendDateStr, bo.getSendDateStr());
        lqw.eq(bo.getSendDay() != null, IndustrialReportTask::getSendDay, bo.getSendDay());
        lqw.eq(bo.getSendMonth() != null, IndustrialReportTask::getSendMonth, bo.getSendMonth());
        lqw.eq(StringUtils.isNotBlank(bo.getSendTime()), IndustrialReportTask::getSendTime, bo.getSendTime());
        lqw.eq(bo.getValidityPeriod()!=null, IndustrialReportTask::getValidityPeriod, bo.getValidityPeriod());
        lqw.eq(StringUtils.isNotBlank(bo.getReportTemplate()), IndustrialReportTask::getReportTemplate, bo.getReportTemplate());
        lqw.eq(bo.getDataStatus() != null, IndustrialReportTask::getDataStatus, bo.getDataStatus());

        lqw.like(StringUtils.isNotBlank(bo.getCreateBy()), IndustrialReportTask::getCreateBy, bo.getCreateBy());
        lqw.like(StringUtils.isNotBlank(bo.getUpdateBy()), IndustrialReportTask::getUpdateBy, bo.getUpdateBy());
        lqw.between(params.get("beginCreateTime") != null && params.get("endCreateTime") != null,
            IndustrialReportTask::getCreateTime, params.get("beginCreateTime"), params.get("endCreateTime"));
        lqw.between(params.get("beginUpdateTime") != null && params.get("endUpdateTime") != null,
            IndustrialReportTask::getCreateTime, params.get("beginUpdateTime"), params.get("endUpdateTime"));
        return lqw;
    }

    /**
     * 新增填报任务
     */
    @Override
    public Boolean insertByBo(IndustrialReportTaskBo bo) {
        IndustrialReportTask add = BeanUtil.toBean(bo, IndustrialReportTask.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改填报任务
     */
    @Override
    public Boolean updateByBo(IndustrialReportTaskBo bo) {
        IndustrialReportTask update = BeanUtil.toBean(bo, IndustrialReportTask.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(IndustrialReportTask entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除填报任务
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
